package com.xxx.controller;


import com.xxx.entity.Category;
import com.xxx.entity.Goods;
import com.xxx.service.CategoryService;
import com.xxx.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("/admin/goods")
@Controller
public class AdminGoodsController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private GoodsService goodsService;


    @RequestMapping("/list")
    public String list(Model model){
        List<Goods> goodsList = goodsService.findAll();
        model.addAttribute("goodsList",goodsList);
        return "/admin/goods_list";
    }


    @RequestMapping("/update")
    public String update(Integer goodsId, Model model){
        Goods goods = goodsService.find(goodsId);
        List<Category> categoryList = categoryService.list();
        model.addAttribute("goods",goods);
        model.addAttribute("categoryList",categoryList);
        return "/admin/goods_update";
    }




    @RequestMapping("/add")
    public String add(Model model){
        List<Category> categoryList = categoryService.list();
        model.addAttribute("categoryList",categoryList);
        return "/admin/goods_add";
    }


    @RequestMapping("/add_submit")
    public String add_submit(Goods goods, MultipartFile file){
        String imgFile = null;
        // 判断是否上传了图片    没上传使用默认图片
        if (file.isEmpty()){
            imgFile = "/img/default.jpg";
        }
        // 生成新的文件名
        String newFile = UUID.randomUUID() + "-" + file.getOriginalFilename();
        File f = new File("D:\\资料\\壁纸",newFile);

        // 判断上级文件夹是否存在，不存在则创建
        if (!f.getParentFile().exists()){
            f.mkdirs();
        }

        try {
            // 将上传的文件存到指定磁盘
            file.transferTo(f);
            // 记录上传文件的web访问路径
            imgFile = "/upfile/"+newFile;
        } catch (IOException e) {
            e.printStackTrace();
        }

        goods.setPicture(imgFile);
        goodsService.add(goods);

        return "redirect:/admin/goods/list";

    }


    @RequestMapping("/update_submit")
    public String update_submit(Goods goods, MultipartFile file){

        if (file.isEmpty()){
            // 生成新的文件名
            String newFile = UUID.randomUUID() + "-" + file.getOriginalFilename();
            File f = new File("D:\\资料\\壁纸",newFile);

            // 判断上级文件夹是否存在，不存在则创建
            if (!f.getParentFile().exists()){
                f.mkdirs();
            }

            try {
                // 将上传的文件存到指定磁盘
                file.transferTo(f);
                goods.setPicture("/upfile/"+newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        goodsService.update(goods);

        return "redirect:/admin/goods/list";

    }
}
